package cn.yj.core.tuodan;

import cn.yj.annotation.pagehelper.page.OrderBy;
import cn.yj.cgs.dba.entity.TuoDan;
import cn.yj.commons.utils.MapUtils;
import cn.yj.config.web.AppBaseController;
import cn.yj.entity.R;
import cn.yj.tools.exception.ServiceException;
import cn.yj.tools.readconfig.PropertiesUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-10-10 18:54
 */
@RestController
@RequestMapping("/tuodan")
public class TuoDanController extends AppBaseController {


    @Resource
    private TuoDanService service;

    @GetMapping("/myPush")
    public R myPush() {
        return success(service.myPush(getMap("userCode", getUserCode()), page(new OrderBy(OrderBy.Direction.DESC,
                "create_time"))));
    }

    @PostMapping("/push")
    public R push(@RequestBody TuoDan tuoDan) {
        tuoDan.setUserCode(getUserCode());
        return success(service.push(tuoDan));
    }

    @GetMapping("/myGetList")
    public R myGetList() {
        return success(service.myGetList(getUserCode(), page(new OrderBy(OrderBy.Direction.DESC, "td.create_time"))));
    }

    @DeleteMapping("/delete/{code}")
    public R delete(@PathVariable("code") String tuodanCode) {
        return result(service.delete(tuodanCode));
    }

    @GetMapping("/random")
    public R random(String schoolName, Integer sex) {
        return success(service.random(getUserCode(), schoolName, sex));
    }

    @PostMapping("/toPayPush")
    public R toPayPush() {
        try {
            return success(service.toPayPushOrder(getUserCode(), getOpenId()));
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("支付异常,请重试");
        }
    }

    @PostMapping("/toPay")
    public R toPay(@RequestBody Map<String, Object> body) {
        try {
            return success(service.toPayOrder(getUserCode(), MapUtils.getStringVal(body, "schoolName"),
                    MapUtils.getIntVal(body, "sex"), getOpenId()));
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("支付异常,请重试");
        }
    }

    /**
     * 支付信息
     */
    @GetMapping("/config")
    public R config() {
        Map<String, Object> map = getMap();
        boolean booleanValue = PropertiesUtils.getBooleanValue("wechat.pay.start");
        map.put("pushStart", booleanValue?PropertiesUtils.getBooleanValue("push.start"):booleanValue);
        map.put("pushPay", Double.parseDouble(PropertiesUtils.getStringValue("push.pay")) / 100);
        map.put("pullStart", booleanValue?PropertiesUtils.getBooleanValue("pull.start"):booleanValue);
        map.put("pullPay", Double.parseDouble(PropertiesUtils.getStringValue("pull.pay")) / 100);
        return success(map);
    }
}
